﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BC;
using CM.BL.BE;

namespace WebAppHTML5
{
    public partial class release : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pRetval = "";
            if (!Request.HttpMethod.Equals("POST"))
            {

            }
            else
            {
                pRetval = "{\"success\":{0}, \"errors\":{\"reason\": \"{1}\"},  ";
                pRetval += "\"oLib\": \"{2}\" ";
                pRetval += "}";
                string _retval = "";
                bool result = releasex(out _retval);
                if (result)
                {
                    pRetval = pRetval.Replace("{0}", "true");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", result.ToString());
                }
                else
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", _retval);
                    pRetval = pRetval.Replace("{2}", result.ToString());
                }
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);

            //string pRetval2 = "";
            //pRetval2 = "{\"success\":true, \"errors\":{\"reason\": \"El pedido 4700000673 ha sido liberado.\"},  \"oLib\": \"True\" }";
            //Response.Write(pRetval2);

        }

        private bool releasex(out string _retval)
        {
            string strpo, strco, retval = ""; bool oLib = false;
            string obj = "";
            strpo = Request.Params["PO"].ToString();
            strco = Request.Params["LibCod"].ToString();

            if (!string.IsNullOrEmpty(strpo) && !string.IsNullOrEmpty(strco))
            {
                CredencialesController.cargarInfo(Request.Params); //(SIEMPRE) @@@
                string SesID = "";
                if (string.IsNullOrEmpty(Request.Params["Id"]))
                {
                    SesID = Session.SessionID;
                }
                else
                {
                    SesID = Request.Params["Id"];
                }
                //************************************************************* @@@
                retval = PurchaseOrderBC.getInstance(SesID).releasePO(strpo, strco, out oLib);
            }

            _retval = retval;
            return oLib;
        }
    }
}
